package com.example.app05;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.app05.activity.AddActivity;
import com.example.app05.activity.DetailsActivity;
import com.example.app05.adapter.TaskAdapter;
import com.example.app05.dao.MyDatabaseHelper;
import com.example.app05.dao.TaskDao;
import com.example.app05.entity.Task;
import com.example.app05.service.PushService;
import com.example.app05.tool.Tool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: cx
 * @date: 2022/5/1
 */
public class MainActivity extends AppCompatActivity {
    private List<Task> taskList = new ArrayList<>();
    private TaskAdapter taskAdapter;
    private PushService.MyBinder myBinder;
    private MyServiceConnection myServiceConnection;


    //创建数据库所用参数
    private SQLiteDatabase db;
    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_list);
        initDatabaseByDBHelper();
        taskList = TaskDao.queryData(db);
        startAndBindService(taskList);
        initView();
        toAddTask();
    }
    public void toAddTask(){
        Button btnAdd = findViewById(R.id.tl_btn);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到添加页面
                Tool.onItemClick(MainActivity.this, AddActivity.class,null);
                MainActivity.this.finish();
            }
        });
    }
    public void startAndBindService(List<Task> taskList){
        Intent intent = new Intent(
                MainActivity.this,
                PushService.class
        );
        intent.putExtra("size",taskList.size());
        for(int i=0;i<taskList.size();i++){
            intent.putExtra("task"+i,taskList.get(i));
        }
        startService(intent);
        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                }catch (Exception e){
                    e.printStackTrace();
                }
                myServiceConnection = new MyServiceConnection();
                bindService(intent,myServiceConnection,BIND_AUTO_CREATE);
            }
        }.start();


    }
    //创建数据库
    public void initDatabaseByDBHelper() {
        //创建数据库管理类的对象
        dbHelper = new MyDatabaseHelper(
                this,"task_table",null,1
        );
        //获得数据库对象
        db = dbHelper.getWritableDatabase();
    }
    private void initView(){
        taskAdapter = new TaskAdapter(this,taskList,R.layout.task);
        ListView taskListView = findViewById(R.id.tl_list);
        taskListView.setAdapter(taskAdapter);
        taskListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.e("任务标题",taskList.get(i).getTopic());
                Tool.onItemClick(MainActivity.this, DetailsActivity.class,taskList.get(i));
                MainActivity.this.finish();
            }
        });
        taskListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("温馨提示")
                        .setMessage("是否删除数据")
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                TaskDao.deleteData(db,taskList.get(i).getId());
                                Tool.onItemClick(MainActivity.this,MainActivity.class,null);
                                MainActivity.this.finish();
                            }
                        }).create();
                alertDialog.show();
                return false;
            }
        });
    }


    private class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //当绑定服务成功以后执行
            Log.i("MyBindService","onServiceConnected");
            myBinder = (PushService.MyBinder) service;
            myBinder.print();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //断开连接后执行（非正常）
            Log.i("MyBindService","onServiceDisconnected");
        }
    }

    @Override
    protected void onDestroy() {
        unbindService(myServiceConnection);
        super.onDestroy();
    }
}